import sys,os,time


ACTION_DICT={}
ACTION_DICT["REST"]={}
ACTION_DICT["REST"]["ACTION_REST_PUT"]="REST_PUT"
ACTION_DICT["REST"]["ACTION_REST_GET"]="REST_GET"
ACTION_DICT["REST"]["ACTION_REST_POST"]="REST_POST"
ACTION_DICT["REST"]["ACTION_REST_DEL"]="REST_DEL"
ACTION_DICT["REST"]["ACTION_REST_ALL"]="REST_ALL"

CONDITION_DICT={}
CONDITION_DICT["VALUE"]={}
CONDITION_DICT["OP"]={}

CONDITION_DICT["VALUE"]["RULE_COUNT"]="RULE_COUNT"
CONDITION_DICT["VALUE"]["REQUEST_DATA_SIZE"]="REQ_DATA_SIZE"
CONDITION_DICT["VALUE"]["REQUEST_DATA_TOTAL"]="REQ_DATA_TOTAL"
CONDITION_DICT["VALUE"]["REQUEST_COUNT"]="REQ_COUNT"
CONDITION_DICT["VALUE"]["CONN_NO"]="CONN_NO"
CONDITION_DICT["VALUE"]["SYS_TIME"]="SYS_TIME"

CONDITION_DICT["OP"]["="]="="
CONDITION_DICT["OP"]["!"]="!"
CONDITION_DICT["OP"]["<"]="<"
CONDITION_DICT["OP"][">"]=">"
CONDITION_DICT["OP"][">="]=">="
CONDITION_DICT["OP"]["<="]="<="

ALLOW=1
DENY=0


class Subject(object):
	"""docstring for Suject"""
	def __init__(self):
		pass
	def add_attr(self,attr_name,attr_value):
		setattr(self,attr_name,attr_value)


class Action(object):
	"""docstring for Action"""
	#actions based on rest API
	"""
	ACTION_REST_PUT="REST_PUT"
	ACTION_REST_GET="REST_GET"
	ACTION_REST_POST="REST_POST"
	ACTION_REST_DEL="REST_DEL"
	ACTION_REST_ALL="REST_ALL"
	"""

	def __init__(self,api_type):
		self.type=api_type
		self.action_list=[]
		for k in ACTION_DICT[self.type]:
			setattr(Action, k, ACTION_DICT[self.type][k])
		self.action_all=[k[1] for k in ACTION_DICT[self.type].items()]
		pass

	def add_action(self,*actions):
		for ac in actions:
			if ac.endswith("ALL"):
				self.action_list=[]
				self.action_list=[k for k in self.action_all if k != ac]
				break
			self.action_list.append(ac)

	def add_not_action(self,*actions):
		if not self.action_list:
			self.action_list=[k for k in self.action_all if not k.endswith("ALL")]
		for ac in actions:
			if ac.endswith("ALL"):
				self.action_list=[]
				break
			self.action_list.remove(ac)

	def remove_action(self,*actions):
		for ac in actions:
			if ac.endswith("ALL"):
				self.action_list=[]
				break
			self.action_list.remove(ac)


	def list_action(self):
		return self.action_list
		
class Condition(object):
	"""docstring for Condition"""
	def __init__(self):
		for l in CONDITION_DICT:
			for k in CONDITION_DICT[l]:
				setattr(Condition, k, CONDITION_DICT[l][k])
		self.cond_dict={}
		self.cond_list=[]

class Policy(object):
	"""docstring for API"""
	def __init__(self,*args):
		self.pid=args[0]
		self.api_type=args[1]
		self.policy_container={}
		self.subject=Subject()
		
		#for k in ACTION_DICT[self.ptype]:
		#	setattr(Action, k, ACTION_DICT[self.ptype][k])
		self.action=Action(self.api_type)

	def add_policy(self):
		print self.subject.__dict__


	class Target(object):
		"""docstring for Target"""
		def __init__(self, arg):
			self.arg = arg
			
	

	class Obligation(object):
		"""docstring for Obligation"""
		ob_print="PRINT"
		def __init__(self, arg):
			self.arg = arg




if __name__ == '__main__':
	#a=API.Attr("test")
	p=Policy("aaa","REST")
	p.subject.name="111"
	p.subject.age=1
	p.subject.time=str(time.time())[2:3]
	p.subject.add_attr("123","123")
	p.action.add_action(Action.ACTION_REST_PUT)
	#p.action.remove_action(Action.ACTION_REST_PUT)
	print p.action.action_list
	c=Condition()
	print dir(c)
	#p.condition.add_cond(Condition.SYS_TIME,"=",10,"ALLOW",)
	#p.condition.add_str_cond("IF SYS_TIME > 10:PRINT;ALLOW")


	
	#p.add_condition()
			
		